package cc.ilzy.mall.user.service.impl;

import cc.ilzy.mall.exceptions.ExceptionEnum;
import cc.ilzy.mall.exceptions.MallException;
import cc.ilzy.mall.model.pojo.Address;
import cc.ilzy.mall.model.pojo.User;
import cc.ilzy.mall.model.request.AddressAddRequest;
import cc.ilzy.mall.model.vo.AddressVo;
import cc.ilzy.mall.user.interceptors.UserLoginInterceptor;
import cc.ilzy.mall.user.mappers.AddressMapper;
import cc.ilzy.mall.user.service.IAddressService;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

@Service
public class AddressServiceImpl  extends ServiceImpl<AddressMapper, Address> implements IAddressService {
    @Override
    public AddressVo findAddressById(Long addressId) {
        User user = UserLoginInterceptor.getCurrentUser();
        Optional<Address> opt = lambdaQuery().eq(Address::getId, addressId)
                                                 .eq(Address::getUserId, user.getId())
                                                 .oneOpt();
        Address address = opt.orElseThrow(() -> new MallException(ExceptionEnum.ADDRESS_NOT_EXIST));
        return BeanUtil.toBean(address, AddressVo.class);
        // AtomicReference<AddressVo> vo = new AtomicReference<>(new AddressVo());
        // opt.ifPresentOrElse(address -> {
        //     vo.set(BeanUtil.toBean(address, AddressVo.class));
        // }, () -> {
        //     throw new MallException(ExceptionEnum.ADDRESS_NOT_EXIST);
        // });
    }

    @Override
    public void add(AddressAddRequest addressRequest) {
        User user = UserLoginInterceptor.getCurrentUser();
        Address address = BeanUtil.toBean(addressRequest, Address.class);
        address.setUserId(user.getId());
        Integer defaultStatus = addressRequest.getDefaultStatus();
        if(defaultStatus != null && defaultStatus == 1){
            // 如果是默认地址，则先将用户的其他地址设置为非默认
            lambdaUpdate().eq(Address::getUserId, user.getId())
                          .set(Address::getDefaultStatus, 0)
                          .update();
            address.setDefaultStatus(1);
        } else {
            // 如果不是默认地址，则设置为非默认
            address.setDefaultStatus(0);
        }
        save(address);
    }

    @Override
    public void del(Long addressId) {
        User user = UserLoginInterceptor.getCurrentUser();
        boolean removed = lambdaUpdate().eq(Address::getId, addressId)
                                         .eq(Address::getUserId, user.getId())
                                         .remove();
        if(!removed) {
            throw new MallException(ExceptionEnum.ADDRESS_NOT_EXIST);
        }
    }

    @Override
    public List<AddressVo> listAllAddress() {
    User user = UserLoginInterceptor.getCurrentUser();
        List<Address> addressList = lambdaQuery().eq(Address::getUserId, user.getId())
                                                 .list();
        return addressList.stream().map(address -> BeanUtil.toBean(address, AddressVo.class)).toList();
    }

}
